/*
 * SqlGenerate.java
 *
 * Created on 30 de Agosto de 2007, 21:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package bd;

/**
 *
 * @author Evolution Systems - Do the Evolution
 */
public class SqlGenerate {
    
    /**
     * Creates a new instance of SqlGenerate
     */
    public SqlGenerate() 
    {
    }
    
    public static String Insert(String table, String fields[], String types[])
    {
        String sql;
        sql = " \" INSERT INTO "
                    + table + "( ";
        for (int i = 0; i<fields.length ; i++)
        {
            sql += fields[i];
            
            if (i != fields.length - 1)
                sql += ", ";
            else
                sql += ") ";
        }
        sql += "VALUES(";
        
        for (int i = 0; i<fields.length; i++)
        {
            if  (types[i].toString().toUpperCase().equals("INT") ||
                 types[i].toString().toUpperCase().equals("INTEGER"))
              sql += "\" + " + fields[i] + " + \" ";
            else
                sql += "'\" + "+fields[i]+" + \"'";
            
            if (i != fields.length - 1)
                sql += ", ";
            else
                sql += ")\" ";
        }
        
        return sql;
    }
    
    public static String Update(String table, String fields[], String types[])
    {
        String sql;
        sql = "\" UPDATE "
                    + table + " "
                    +"SET (";
        for (int i = 0; i <fields.length; i++)
        {
            sql +="\" " + "+ \"" + fields[i] + " = \" " ;
            
            if (types[i].toString().toUpperCase().equals("INTEGER") || 
                types[i].toString().toUpperCase().equals("INTEGER"))
                sql +="+ "+ fields[i] + " + \" ";
            else
                sql+="+ \"'\" + " + fields[i] + " + \"'\" + \" ";
            
            if (i != fields.length - 1)
                sql +=", ";
            else
                sql += ")\" +";
        }
        
        sql += " \" WHERE ("
                  + "\" + \"" + fields[0] + " = \" ";
        if (types[0].toString().toUpperCase().equals("INT") ||
            types[0].toString().toUpperCase().equals("INTEGER"))
            sql +="+ "+ fields[0] + " + \")\"";
        else
            sql+="+ \"'\" + " + fields[0] + " + \"')\"";
        
        return sql;
    }
    
    public static String Delete(String table, String fields[], String types[])
    {
        String sql, where = "";
        sql = "\" DELETE FROM "
                    + table + "\" ";
        if (fields.length > 0)
        {
            where = "+ \" WHERE (";
            for (int i=0; i <fields.length; i++)
            {
                where += "\" + \"" + fields[i] + " = \" ";
                if (types[i].toString().toUpperCase().equals("INT") ||
                    types[i].toString().toUpperCase().equals("INTEGER"))
                    where +="+ "+ fields[i] + " + \"";
                else
                    where+="+ \"'\" + " + fields[i] + " + \"'";
                
                if (i != fields.length - 1)
                    where +=") AND (";
                else
                    where += ")\"";
            }
        }
        
        return sql + where;
    }
    
    public static String Select(String table, String fields[], String whereFields[], String whereTypeFields[])
    {
        String sql, where = "";
        
        sql = "\" SELECT ";      
        for (int i = 0; i<fields.length ; i++)
        {
            sql += fields[i];
            
            if (i != fields.length - 1)
                sql += ", ";
        }
        sql += " FROM "
                + table + " ";
        
        if (whereFields.length > 0)
        {
            where = " WHERE (";
            for (int i=0; i <whereFields.length; i++)
            {
                where += whereFields[i] + " = \" ";
                if (whereTypeFields[i].toString().toUpperCase().equals("INT") || 
                    whereTypeFields[i].toString().toUpperCase().equals("INTEGER"))
                    where +="+ "+ whereFields[i] + " + \"";
                else
                    where+="+ \"'\" + " + whereFields[i] + " + \"'";
                
                if (i != whereTypeFields.length - 1)
                    where +=") AND (";
                else
                    where += ")\"";
            }
        }
        else
         sql += "\"";
        
        return sql + where;
    }
   
}
